<?php

namespace app\label\controller;

use app\common\controller\HomeCommon;
use think\Db;
use think\Cache;

class Index extends HomeCommon
{
    public static $condition=null;
    /**
     * 行业
     */
    public function industry()
    {
        $industry_id = request()->param('industry_id', 0, 'intval');
        $model = \think\Loader::model('Label');
        $data = $model->industry($industry_id);
        return json(['errcode' => '0', 'msg' => lang('Get success'), 'data' => $data]);
    }

    /**
     *
     * 城市
     */
    public function city()
    {
        $city_id = request()->param('city_id', 0, 'intval');
        $model = \think\Loader::model('Label');

        $data = $model->city($city_id);

        return json(['errcode' => '0', 'msg' => lang('Get success'), 'data' => $data]);

    }

    /**
     * 产业链
     *
     */
    public function chain()
    {
        //Cache::clear();die();
        $username = request()->param('username', 'gz', 'trim');
        $users=db('users')->where(['username'=>$username,'status'=>1])->find();

        //p($users);

        if(empty($users)||empty($users['chain_id'])){
            return json(['errcode' => '0', 'msg' => lang('Get error'), 'data' => []]);
        }
        $chain_id = request()->param('chain_id', 0, 'intval');
        $model = \think\Loader::model('Label');
        $data = $model->chain($chain_id,$users['id']);
        return json(['errcode' => '0', 'msg' => lang('Get success'), 'data' => $data]);

    }

    /**
     *产业链节点下的企业有无
     */
    public function chain_com_re()
    {

        $chain_id = request()->param('chain_id', 0, 'intval');
        $reqData['keywords'] = request()->param('keywords', '', 'trim');
        $reqData['industry_id'] = request()->param('industry_id', 0, 'intval');
        $reqData['city_id'] = request()->param('city_id', 0, 'intval');
        $reqData['park_id'] = request()->param('park_id', 0, 'intval');
        $reqData['type'] = request()->param('type', '', 'trim');

        $model = \think\Loader::model('Label');
        $data = $model->chain_show($chain_id, $reqData);
        return json(['errcode' => '0', 'msg' => lang('Get success'), 'data' => $data]);

        $id = request()->param('chain_id', 1, 'intval');//哪一条产业链
        $where = "  a.status=1  ";
        /***关键字*/
        $keywords = request()->param('keywords', '', 'trim');
        if (!empty($keywords)) {
            $where .= " and a.company_name like '%$keywords%' ";
        }
        /***行业*/
        $industry_id = request()->param('industry_id', 0, 'intval');
        if (!empty($industry_id)) {
            $obj = $obj->join('companys_industry b', 'a.id=b.company_id');
            $where .= " and b.status=1 and b.industry_id=$industry_id";
        }


        /***城市*/
        $city_id = request()->param('city_id', 0, 'intval');
        if (!empty($city_id)) {
            $where .= " and (a.province_id=$city_id or a.city_id=$city_id or a.area_id=$city_id ) ";
        }
        /***园区*/
        $park_id = request()->param('park_id', 0, 'intval');
        if (!empty($city_id)) {
            $where .= " and a.park_id=$park_id  ";
        }


        $chain = db("chain")->field("id,chain_name")->where(['chain' => $id, "pid" => ['neq', 0]])->select();
        foreach ($chain as $key => $value) {
            # code.

            $info = db('companys a')->join("companys_chain b", "a.id=b.company_id")->field("a.id")->where($where . " and b.chain_id=" . $value['id'])->find();

            if ($info) {
                $chain[$key]['flag'] = 1;
            } else {
                $chain[$key]['flag'] = 0;
            }
        }


        return json(['errcode' => '0', 'msg' => lang('Get success'), 'data' => $chain]);

    }

    /**
     * 产业链和子链的下拉
     */
    public function chain_drop_box()
    {
        $cache = new Cache();
        $result = $cache->get('chain_drop_box');
        if ($result && 1 == 2) {
            $result = unserialize($result);
        } else {
            $chain = db('chain')->where(['pid' => 0, 'status' => 1])->column('id,chain_name,pid,children_chain', 'id');
            $chain_son = db('chain')->where('children_chain', 'neq', 0)->field('id,chain_name,chain,pid,children_chain')->select();
            foreach ($chain_son as $key => $val) {
                $chain[$val['chain']]['children'][] = ['id' => $val['children_chain'], 'chain_name' => $val['chain_name']];
                unset($chain[$val['children_chain']]);
            }
            $result = array();
            foreach ($chain as $item) {
                unset($item['pid'], $item['children_chain']);
                $result[] = $item;
            }
            $id = array_column($result, 'id');
            array_multisort($id, SORT_ASC, $result);
            $cache->set('chain_drop_box', serialize($result), 3600);
        }
        return json(['errcode' => '0', 'msg' => lang('Get success'), 'data' => $result]);
    }

    /**
     * 产业链城市下面的企业和园区个数
     */
    public function chain_city_map()
    {
        $chain_id = request()->param('chain_id', 0, 'intval');
        $city_id = request()->param('city_id', 1, 'intval');
        $type = request()->param('type', '', 'trim');

        $cache = new Cache();
        $result = $cache->get('map_count_chainId:' . $chain_id . ':cityId:' . $city_id . ':type:' . $type);
        if ($result && 1 == 2) {
            $result = unserialize($result);
        } else {
            if ($type == 'company') {
                $obj = db('companys a')->where('a.status', 1);
                /*****城市***/
                $city_data = db('city')->where('pid', $city_id)->field('id,city_name,lat,lng,level')->select();
                $result = array();
                if (!empty($city_id)) {
                    $city_type = db('city')->where('id', $city_id)->field('type,special')->find();
                    switch ($city_type['type']) {
                        case 0:
                            /*****国家***/
                            foreach ($city_data as $pro) {
                                /*****产业链***/
                                if (!empty($chain_id)) {
                                    $obj->join('companys_chain b', 'a.id=b.company_id', 'LEFT')->where('b.status', 1)->where('b.chain_id', $chain_id);
                                }
                                $total = $obj->where('a.province_id', $pro['id'])->count();
                                $pro['total'] = $total;
                                if ($total != 0){
                                    $result[] = $pro;
                                }
                            }
                            break;
                        case 1:
                            if ($city_type['special']==1){
                                /*****直轄市或特殊省份***/
                                $special=[];
                                foreach ($city_data as $pro) {
                                    $special = db('city')->where('pid', $pro['id'])->field('id,city_name,lat,lng,level')->select();
                                }
                                foreach ($special as $pro) {
                                    /*****产业链***/
                                    if (!empty($chain_id)) {
                                        $obj->join('companys_chain b', 'a.id=b.company_id', 'LEFT')->where('b.status', 1)->where('b.chain_id', $chain_id);
                                    }
                                    $total = $obj->where('a.area_id', $pro['id'])->count();
                                    $pro['total'] = $total;
                                    if ($total != 0){
                                        $result[] = $pro;
                                    }
                                }
                            }else{
                                /*****普通省份***/
                                foreach ($city_data as $pro) {
                                    /*****产业链***/
                                    if (!empty($chain_id)) {
                                        $obj->join('companys_chain b', 'a.id=b.company_id', 'LEFT')->where('b.status', 1)->where('b.chain_id', $chain_id);
                                    }
                                    $total = $obj->where('a.city_id', $pro['id'])->count();
                                    $pro['total'] = $total;
                                    if ($total != 0){
                                        $result[] = $pro;
                                    }
                                }
                            }
                            break;
                        case 2:
                            /*****城市***/
                            if (!empty($chain_id)) {
                                $obj->join('companys_chain b', 'a.id=b.company_id', 'LEFT')->where('b.status', 1)->where('b.chain_id', $chain_id);
                            }
                            $result = $obj->where('a.city_id', $city_id)->field('a.identification as id,a.company_name,a.lat,a.lng')->select();
                            break;
                        case 3:
                            /*****区县（现在主要是直辖市）***/
                            if (!empty($chain_id)) {
                                $obj->join('companys_chain b', 'a.id=b.company_id', 'LEFT')->where('b.status', 1)->where('b.chain_id', $chain_id);
                            }
                            $result = $obj->where('a.area_id', $city_id)->field('a.identification as id,a.company_name,a.lat,a.lng')->select();
                            $total=count($result);
                            break;
                        default:
                            exit();
                    }
                }
            } elseif ($type == 'park') {

                /*****城市***/
                $obj = db('parks a')->where('a.status', 1);
                $city_data = db('city')->where('pid', $city_id)->field('id,city_name,lat,lng,level')->select();
                $result = array();
                if (!empty($city_id)) {
                    $city_type = db('city')->where('id', $city_id)->field('type,special')->find();
                    switch ($city_type['type']) {
                        case 0:
                            /*****国家***/
                            foreach ($city_data as $pro) {
                                /*****产业链***/
                                if (!empty($chain_id)) {
                                    $obj->join('parks_chain b', 'a.id=b.park_id', 'LEFT')->where('b.status', 1)->where('b.chain_id', $chain_id);
                                }
                                $total = $obj->where('a.province_id', $pro['id'])->count('a.id');
                                $pro['total'] = $total;
                                if ($total != 0){
                                    $result[] = $pro;
                                }
;                            }
                            break;
                        case 1:
                            if ($city_type['special']==1){
                                /*****直轄市或特殊省份***/
                                $special=[];
                                foreach ($city_data as $pro) {
                                    $special = db('city')->where('pid', $pro['id'])->field('id,city_name,lat,lng,level')->select();
                                }
                                foreach ($special as $pro) {
                                    /*****产业链***/
                                    if (!empty($chain_id)) {
                                        $obj->join('parks_chain b', 'a.id=b.park_id', 'LEFT')->where('b.status', 1)->where('b.chain_id', $chain_id);
                                    }
                                    $total = $obj->where('a.area_id', $pro['id'])->count();
                                    $pro['total'] = $total;
                                    if ($total != 0){
                                        $result[] = $pro;
                                    }
                                }
                            }else{
                                /*****省份***/
                                foreach ($city_data as $pro) {
                                    /*****产业链***/
                                    if (!empty($chain_id)) {
                                        $obj->join('parks_chain b', 'a.id=b.park_id', 'LEFT')->where('b.status', 1)->where('b.chain_id', $chain_id);
                                    }
                                    $total = $obj->where('a.city_id', $pro['id'])->count();
                                    $pro['total'] = $total;
                                    if ($total != 0){
                                        $result[] = $pro;
                                    }
                                }
                            }
                            break;
                        case 2:
                            /*****城市***/
                            /*****产业链***/
                            if (!empty($chain_id)) {
                                $obj->join('parks_chain b', 'a.id=b.park_id', 'LEFT')->where('b.status', 1)->where('b.chain_id', $chain_id);
                            }
                            $result = $obj->where('a.city_id', $city_id)->field('a.identification as id,a.park_name,a.lat,a.lng')->select();
                            break;
                        case 3:
                            /*****区县（现在主要是直辖市）***/
                            if (!empty($chain_id)) {
                                $obj->join('parks_chain b', 'a.id=b.park_id', 'LEFT')->where('b.status', 1)->where('b.chain_id', $chain_id);
                            }
                            $result = $obj->where('a.area_id', $city_id)->field('a.identification as id,a.park_name,a.lat,a.lng')->select();
                            $total=count($result);
                            break;
                        default:
                            exit();
                    }
                }
            } else {
                return false;
            }
            $cache->set('map_count_chainId:' . $chain_id . ':cityId:' . $city_id . ':type:' . $type, serialize($result), 3600);

            $info = [];
            if (!empty($city_id)) {
                $info = db('city')->find($city_id);
                $info['total']=empty($total) ? 0 : $total;
            }
        }

        return json(['errcode' => '0', 'msg' => lang('Get success'), 'data' => $result, 'info' => $info]);
    }
}
